package com.thinkingstar.iads.datacenter.entity.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.formula.functions.T;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.datacenter.entity.DcProjectPutFord;


/**
 * @ClassName: DaoDcProjectPutFord 
 * @Description:数据中心_项目_提出资料维护
 * @author 1157372219@qq.com 
 * @date 2018年3月19日   
 */
@IocBean(args = {"refer:dao"})
public class DaoDcProjectPutFord extends EntityRepositoryServiceImpl<DcProjectPutFord>{

	public DaoDcProjectPutFord(Dao dao) {
		super(dao);
	}
	
	public int findProjectPutFordNum(NutMap param){
		StringBuilder stringBuilder = new StringBuilder(" select count(dpp.id) result_num "
				+ " from dc_project_putford dpp "
				+ " where dpp.del_flag = '0' ");
		if(param.get("putford_name")  != null){
			stringBuilder.append(" and dpp.putford_name like @putford_name ");
		}
		Sql sql = Sqls.create(stringBuilder.toString());
		if(param.get("putford_name")  != null){
			sql.params().set("putford_name", param.get("putford_name"));
		}
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				int result_num = 0;
				while (rs.next()) {
					result_num = rs.getInt("result_num");
				}
				return result_num;
			}
		});
		dao().execute(sql);
		return sql.getInt();
	}
	
	//分页查询提出资料信息
	public QueryResult findProjectPutFord(NutMap param){
		List <NutMap> page_list = null;
		Pager pager = null;
		StringBuilder stringBuilder = new StringBuilder(" select dpp.id ,"
				+ " dpp.putford_code , dpp.putford_name , case dpp.putford_type when '0' then '否' else '是' end"
				+ " putford_type , dpdc.project_design_content_name , dpdm.project_design_major_name ,"
				+ " dpdp.project_design_phase_name ,dpp.content_id , dpp.major_id , dpp.phase_id ,"
				+ " dpp.compound_template , dpp.compound_rule ,  "
				+ " dpp.create_time , dpp.update_time   "
				+ " from dc_project_putford dpp "
				+ " left join dc_project_design_content dpdc on dpp.content_id = dpdc.id and dpdc.del_flag = '0'"
				+ " left join dc_project_design_major dpdm on dpp.major_id = dpdm.id and dpdm.del_flag = '0'"
				+ " left join dc_project_design_phase dpdp on dpp.phase_id = dpdp.id and dpdp.del_flag = '0' "
				+ " where dpp.del_flag = '0' ");
		if(param.get("putford_name")  != null){
			stringBuilder.append(" and dpp.putford_name like @putford_name ");
		}
		Sql sql = Sqls.create(stringBuilder.toString());
		if(param.get("putford_name")  != null){
			sql.params().set("putford_name", "%"+param.get("putford_name")+"%");
		}
		if(param.get("page_num") != null && param.get("page_size") != null){
			pager = new Pager(param.getInt("page_num"), param.getInt("page_size"));
			pager.setRecordCount(findProjectPutFordNum(param));
			sql.setPager(pager);
		}
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<NutMap> list = new ArrayList<>();
				while (rs.next()){
					NutMap map = new NutMap();
					ResultSetMetaData metaData = rs.getMetaData();
					int count = metaData.getColumnCount();
					for (int i = count; i > 0; i--) {
						String filed = metaData.getColumnName(i);
						map.put(filed, rs.getString(filed));
					}
					list.add(map);
				}
				return list;
			}
		});
		dao().execute(sql);
		page_list = sql.getList(NutMap.class);
		return new QueryResult(page_list,pager);
	}


	public List<NutMap> getTczlFileList(String design_content_id,String design_phase_id) {
		List <NutMap> list = null;
		StringBuilder find_sql = new StringBuilder("select dpp.*  " +
				" from dc_project_putford dpp " +
				" left join dc_project_design_content dpdc on dpp.content_id = dpdc.id and dpdc.del_flag = '0'" +
				" left join dc_project_design_major dpdm on dpp.major_id = dpdm.id and dpdm.del_flag = '0'" +
				" left join dc_project_design_phase dpdp on dpp.phase_id = dpdp.id and dpdp.del_flag = '0' " +
				" where dpp.del_flag = '0' "
				+ " and dpp.content_id=@design_content_id"
				+ " and dpp.phase_id=@design_phase_id ORDER BY putford_name asc ");
		Sql sql = Sqls.create(find_sql.toString());
		sql.params().set("design_content_id", design_content_id);
		sql.params().set("design_phase_id", design_phase_id);
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<NutMap> list = new ArrayList<>();
				while (rs.next()){
					NutMap nutMap = new NutMap();
					ResultSetMetaData metaData = rs.getMetaData();
					int count = metaData.getColumnCount();
					for (int i = count; i > 0; i--) {
						String filed = metaData.getColumnName(i);
						nutMap.put(filed, rs.getString(filed));
					}
					list.add(nutMap);
				}
				return list;
			}
		});
		dao().execute(sql);
		list = sql.getList(NutMap.class);
		return list;
	}

}
